package com.zhuss.controller.core;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zhuss.bean.bo.core.upload.DeleteUploadBo;
import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.bo.core.upload.UploadBo;
import com.zhuss.common.BaseController;
import com.zhuss.common.UploadOperateService;
import com.zhuss.core.upload.UploadServiceExt;
import com.zhuss.exception.OceanException;
import com.zhuss.exception.ExceptionCode;

/**
 * 上传接口 controller
 * @Author zhushanshuo
   @Date 2020-04-15 11:58
 *
 */
  
@RestController
@RequestMapping("/upload")
public class UploadController extends BaseController {
	
    @Value("${filePath}")
    private String filePath;
    
    @Autowired
	@Qualifier("uploadService")
	private UploadOperateService<Object, Object> uploadService;
	
	@Autowired
	@Qualifier("uploadServiceExt")
	private UploadServiceExt uploadServiceExt;
	
	
	/**
	 * 下载
	 * @param bo
	 * @throws OceanException
	 */
	@ResponseBody
    @PostMapping("/download")
    public Object download(@RequestBody DownloadBo bo) throws OceanException {
		
		Object result = uploadService.download(bo);
		return result;
    }
	
	
	/**
	 * 删除
	 * @param bo
	 * @throws OceanException
	 */
	@ResponseBody
    @PostMapping("/delete")
    public void delete(@RequestBody DeleteUploadBo bo) throws OceanException {
		
		uploadService.delete(bo);
    }


    @ResponseBody
    @PostMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile file) throws OceanException {
        if (file.isEmpty()) {
        	throw new OceanException(ExceptionCode.请选择文件);
        }
        
        // String contentType = file.getContentType();
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."), fileName.length());
        String uuid = UUID.randomUUID().toString();
        String key = uuid.replaceAll("-", "") + suffixName;
        
        UploadBo bo = new UploadBo(fileName, suffixName, key, filePath, file);
        Object result = uploadService.upload(bo);
        return result;
        
        
        /*try {
            FileResourceController.uploadFile(file.getBytes(), filePath, key);
            returnable.successFile(key + "," + fileName.substring(0, fileName.lastIndexOf(".")), contentType);
        } catch (Exception e) {
            e.printStackTrace();
            returnable.failure(fileName);
        }
        return returnable;*/

        
        /*String fileName = file.getOriginalFilename();
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            LOGGER.info("上传成功");
        } catch (IOException e) {
            LOGGER.error(e.toString(), e);
            throw new OceanException(ExceptionCode.上传失败);
        }*/
    }

    
}

